package com.xcjw.com.Utils;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @packname com.homeclient.com.Utils
 * @filename TimeFormatUtils
 * @date on 2018/8/13 13:41
 *****/
public class TimeFormatUtils {

      private static final long M24HOURMS = 86400000;

      //毫秒转换未时间
      public static String ms2Date(long _ms) {
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return format.format(date);
      }

      public static String ms2sf(long _ms) {
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return format.format(date);
      }


      public static String ms2DIAN(long _ms) {
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
            return format.format(date);
      }

      public static String ms2DateOnlyDay(long _ms) {
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(date);
      }
      public static String ms2week(long _ms) {
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("EEEE");
            return format.format(date);
      }

      public static String datetime(long ms, long _ms) {
            long time = ms - _ms / 1000;
            if (time <= 60) {
                  return "刚刚";
            }
            if (time > 60 && time <= 3600) {
                  return time / 1000 + "分钟前";
            }
            if (time > 3600 && time <= 3600 * 24) {
                  return time / 3600 + "小时前";
            }
            if (time > 3600 * 24 && time < 3600 * 24 * 2) {
                  return "昨天";
            }
            if (time > 3600 * 24 * 2) {
                  return TimeFormatUtils.ms2DIAN(_ms);
            }

            return time + "";
      }

      public static long formatestring(String s) throws ParseException {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = formatter.parse(s);
            return date.getTime();
      }
      public static long formatestringval(String s,String val) throws ParseException {
            SimpleDateFormat formatter = new SimpleDateFormat(val);
            Date date = formatter.parse(s);
            return date.getTime();
      }
      public static String ms2nian(long _ms) {
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日-HH:mm");
            return format.format(date);
      }

      public static int compare_date(String DATE1, String DATE2) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            try {
                  Date dt1 = df.parse(DATE1);
                  Date dt2 = df.parse(DATE2);
                  if (dt1.getTime() > dt2.getTime()) {
                        System.out.println("dt1 在dt2前");
                        return 1;
                  } else if (dt1.getTime() < dt2.getTime()) {
                        System.out.println("dt1在dt2后");
                        return -1;
                  } else {
                        return 0;
                  }
            } catch (Exception exception) {
                  exception.printStackTrace();
            }
            return 0;
      }

      public static List<Long> getWeekDayList(String date, String formatSrt) {
            // 存放每一天时间的集合
            List<Long> weekMillisList = new ArrayList<Long>();
            long dateMill = 0;
            try {
                  // 获取date的毫秒值
                  dateMill = formatestringval(date,formatSrt);
            } catch (ParseException e) {
                  e.printStackTrace();
            }
            // Calendar
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(dateMill);
            // 本周的第几天
            int weekNumber = calendar.get(Calendar.DAY_OF_WEEK);
            Log.e("本周第几天", weekNumber + "");
            // 获取本周一的毫秒值
            long mondayMill = dateMill - M24HOURMS * (weekNumber - 2);

            for (int i = 0; i < 7; i++) {
                  weekMillisList.add(mondayMill + M24HOURMS * i);
            }
            return weekMillisList;
      }


}